package edu.xidian.bdilab.jrbigdata.service.bank.register.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.domain.BankInfo;
import edu.xidian.bdilab.jrbigdata.domain.BankUser;
import edu.xidian.bdilab.jrbigdata.mapper.BankInfoMapper;
import edu.xidian.bdilab.jrbigdata.mapper.BankUserMapper;
import edu.xidian.bdilab.jrbigdata.service.bank.register.BankRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName BankRegisterServiceImpl
 * @Description TODO
 * @Author tuantuan
 * @Date 2019/3/28 下午7:15
 * @Version 1.0
 * @Attention Copyright (C), 2004-2019, BDILab, XiDian University
 **/
@Service
public class BankRegisterServiceImpl implements BankRegisterService {
    @Autowired
    BankInfoMapper bankInfoMapper;
    @Autowired
    BankUserMapper bankUserMapper;


    /**
     * 查询用户名是否已经注册
     * @param fkUserName
     * @return
     */
    @Override
    public boolean checkUserName(String fkUserName) {
        QueryWrapper<BankUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_name", fkUserName);
        BankUser bankUser = bankUserMapper.selectOne(userQueryWrapper);
        if (bankUser == null) {
            return true;
        }
        return false;
    }


    /**
     * 通过银行ID查看该银行是否已经注册
     *
     * @param bankId
     * @return
     */
    @Override
    public boolean checkBankId(int bankId) {
        QueryWrapper<BankInfo> infoQueryWrapper = new QueryWrapper<>();

        infoQueryWrapper.eq("bank_id", bankId);
        BankInfo bankInfo = bankInfoMapper.selectOne(infoQueryWrapper);
        if (bankInfo == null) {
            return true;
        }
        return false;
    }



    /**
     * 将金融机构信息插入机构信息表
     *
     * @param bankId
     * @param bankName
     * @param regin
     * @param street
     * @param contactName
     * @param contactPhone
     * @param description
     */
    @Override
    public void setBankInfo(int bankId, String bankName, int regin, String street, String contactName, String contactPhone, String fkUserName, String description) {
        BankInfo bankInfo = new BankInfo();
        bankInfo.setBankId(bankId);
        bankInfo.setBankName(bankName);
        bankInfo.setRegin(regin);
        bankInfo.setStreet(street);
        bankInfo.setContactName(contactName);
        bankInfo.setContactPhone(contactPhone);
        bankInfo.setFkUserName(fkUserName);
        bankInfo.setDescription(description);

        bankInfoMapper.insert(bankInfo);
    }

    /**
     * 将注册填写的用户名与密码插入BankUser表中
     *
     * @param fkUserName
     * @param password
     */
    @Override
    public void setBankUser(String fkUserName, String password) {
        BankUser bankUser = new BankUser();

        bankUser.setUserName(fkUserName);
        bankUser.setPassword(password);
        bankUserMapper.insert(bankUser);

    }
}

